package com.atguigu.daedlock;

import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;

import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.locks.ReentrantLock;
import java.util.function.Consumer;

public class DeadLock02 {

    static ReentrantLock lock1 = new ReentrantLock();
    static ReentrantLock lock2 = new ReentrantLock();


    public static void main(String[] args) {
        new Thread(()->{
            lock1.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"线程一进行");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                lock2.lock();
                try {
                    System.out.println(Thread.currentThread().getName()+"线程一抢锁");
                } finally {
                    lock2.unlock();
                }
            } finally {
                    lock1.unlock();
            }

        },"1").start();


        new Thread(()->{
            lock2.lock();
            try {
                System.out.println(Thread.currentThread().getName()+"线程二进行");
                try {
                    Thread.sleep(10);
                } catch (InterruptedException e) {
                    throw new RuntimeException(e);
                }
                lock1.lock();
                try {
                    System.out.println(Thread.currentThread().getName()+"线程二抢锁");
                } finally {
                    lock1.unlock();
                }
            } finally {
                lock2.unlock();
            }

        },"2").start();
    }
}
